package com.flk.oauth.config;

import com.flk.oauth.vaildatecode.processor.NeedValidateImageException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xxwy
 * on 2018/10/17 0017
 */
@ControllerAdvice
public class ExceptionConfig {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionConfig.class);


    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public ResponseEntity<ModelMap> getExceptionInfo(HttpServletRequest request, HttpServletResponse response,
                                                     Exception ex) {
        logger.info(request.getRequestURL() + " error==>{}", ex.getMessage());
        //1 参数校验异常（需要从绑定结果里面取出异常以及异常信息）
        BindingResult bindingResult = ((MethodArgumentNotValidException) ex).getBindingResult();
        ModelMap modelMap = new ModelMap();
        Map<String,String> msg = new HashMap<>();
        if (bindingResult.hasErrors()) {
            List<FieldError> list = bindingResult.getFieldErrors();
            for (FieldError fieldError : list) {
                msg.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
        }
        modelMap.put("code",HttpStatus.BAD_REQUEST.value());
        modelMap.put("error",msg);
        modelMap.put("timestamp", System.currentTimeMillis());
        modelMap.put("path",request.getRequestURL());
        return ResponseEntity.ok(modelMap);
    }



    /**
     * 异常处理
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity<ModelMap> exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception {

        logger.info(request.getRequestURL() + " error==>{}", ex.getMessage());
        ModelMap modelMap = new ModelMap();
        if(ex instanceof NeedValidateImageException ){
            modelMap.put("code", HttpStatus.BAD_REQUEST.value());
            modelMap.put("reqI",true);
            modelMap.put("msg", ex.getMessage());
        }
        else if (ex.getClass().getName().startsWith("org.springframework")) {
            modelMap.put("code", HttpStatus.BAD_REQUEST.value());
            String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpStatus.BAD_REQUEST.getReasonPhrase());
            modelMap.put("msg", msg);
        } else {
            logger.error("系统错误：");
            ex.printStackTrace();
            modelMap.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
            String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            modelMap.put("msg", msg.length() > 150 ? "Ծ‸Ծ，我迷路了" : msg);
        }
        modelMap.put("timestamp", System.currentTimeMillis());
        modelMap.put("path",request.getRequestURL());
        return ResponseEntity.ok(modelMap);
    }


}
